<?php

/**
 * Subclass for representing a row from the 'news' table.
 *
 * 
 *
 * @package lib.model
 */ 
class News extends BaseNews
{
    public function hydrate(ResultSet $rs, $startcol = 1)
    {
        $this->setCulture(sfContext::getInstance()->getUser()->getCulture());
        parent::hydrate($rs, $startcol);
    }

    /**
     * Returns whether Remarks module enabled in news
     *
     * @return boolean
     */
    public static function isRemarksEnabled()
    {
        return sfConfig::get('mod_news_enable_remarks', 0) != 0;
    }
    /**
     * returns allowable number of photos for one new
     */
    public static function allowableNumberPhotos()
    {
        return sfConfig::get('mod_news_number_of_photos', 1);
    }
    /**
     *  returns news number dispalyed on page
     */
    public static function getNewsOnPage()
    {
        return sfConfig::get('mod_news_number_of_photos', 10);
    }

    public static function sendMail($senderMail, $senderName, $recipients, $subject, $body)
    {

        $mail = new sfMail();
        $mail->initialize();
        $mail->addCustomHeader('MIME-Version', '1.0');
        $mail->addCustomHeader('Content-type', 'text/html;charset=utf-8');

        $mail->setCharset('utf-8');

        // definition of the required parameters
        $mail->setSender($senderMail, $senderName);
        $mail->setFrom($senderMail, $senderName);

        if (is_array($recipients))
            $mail->addAddresses($recipients);
        else
            $mail->addAddress($recipients);

        $mail->setSubject($subject);
        $mail->setBody($body);
        //debugvar($mail);
        //die();
        // send the email
        $mail->send();

    }

    public function getStatusName()
    {
        return MyConstants::getStatusName($this->status_id);
    }
    
    public function getRemarksNumber()
    {
        return Remarks::getRemarksNumber(MyConstants::TYPE_NEWS,$this->getId());
    }

    public static function getImage($id, $size)
    {
        $c = new Criteria();
        $c->add(FilesPeer::OBJ_TYPE, MyConstants::TYPE_NEWS);
        $c->add(FilesPeer::OBJ_ID, $id);
        $newsImg = FilesPeer::doSelectOne($c);
        if( $newsImg )
        {
            $image = '/images/gallery/' . $size . '/' . $newsImg->getUname();
        }
        else
        {
            $image = '/images/news_logo.jpg';
        }
        return $image;
    }
    /**
     * deletes files by deleting news
     * @param unknown_type $con
     */
    public function delete($con = null)
    {
    	$files = Files::getByObj(MyConstants::TYPE_NEWS, $this->getId(), get_user_id());
    	foreach( $files as $file )
    	{
    		/* @var $file Files */
    		Files::deleteByUname($file->getUname());
    	}
    	parent::delete($con);
    }
}
